package com.demo.juc.inter;

/**
 * 资源类:
 * 高内聚、低耦合
 * 判断、干活、通知
 *
 * 判断时 if 改成while
 *
 */
class Scorce {

    private int number = 0;

    public synchronized void increase() throws InterruptedException {
        //判断
        /*if (number != 0) {
            this.wait();
        }*/
        while (number != 0) {
            this.wait();
        }
        //干活
        number++;
        System.out.println(Thread.currentThread().getName() + ":\t" + number);

        //通知
        this.notifyAll();
    }

    public synchronized void decrease() throws InterruptedException {
        /*if (number == 0) {
            this.wait();
        }*/
        while (number == 0) {
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName() + ":\t" + number);
        this.notifyAll();
    }

}
